package com.open.server.api.system;

import com.open.base.utils.UserHolderUtils;
import com.open.common.model.Page;
import com.open.common.model.PageRequest;
import com.open.common.model.Result;
import com.open.system.dispatch.INoticeDispatch;
import com.open.system.dispatch.IUserNoticeDispatch;
import com.open.system.model.Notice;
import com.open.system.model.UserNotice;
import com.open.system.model.dto.SendDto;
import com.open.system.model.vo.NoticeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 通知公告
 * @author 大树03
 * @date 2025-07-18 17:38
 */
@RestController
@RequestMapping("/system/notice")
public class NoticeApi{

    @Autowired
    private INoticeDispatch noticeDispatch;

    @Autowired
    private IUserNoticeDispatch userNoticeDispatch;
    /**
     * 对象添加
     * @param model 添加的对象
     * @return
     */
    @PostMapping("save")
    public Result<Boolean> save(@RequestBody Notice model)  {
        long now=System.currentTimeMillis();
        Result<Boolean> result=new Result<>();
        result.setData(noticeDispatch.save(model));
        result.setTime(System.currentTimeMillis()-now);
        return result;
    }

    /**
     * 对象修改
     * @param model 修改的对象
     * @return
     */
    @PostMapping("update")
    public Result<Boolean> update(@RequestBody Notice model)  {
        long now=System.currentTimeMillis();
        Result<Boolean> result=new Result<>();
        result.setData(noticeDispatch.update(model));
        result.setTime(System.currentTimeMillis()-now);
        return result;
    }

    /**
     * 根据对象列表查询
     * @param model 查询条件
     * @return
     */
    @PostMapping("findByModel")
    public Result<List<Notice>> findByModel(@RequestBody Notice model)  {
        long now=System.currentTimeMillis();
        Result<List<Notice>> result=new Result<>();
        result.setData(noticeDispatch.findByModel(model));
        result.setTime(System.currentTimeMillis()-now);
        return result;
    }

    /**
     * 根据主键查询对象
     * @param id 查询的对象主键
     * @return
     */
    @GetMapping("findByPk")
    public Result<Notice> findByPk(@RequestParam("id") Long id)  {
        long now=System.currentTimeMillis();
        Result<Notice> result=new Result<>();
        result.setData(noticeDispatch.findByPk(id));
        result.setTime(System.currentTimeMillis()-now);
        return result;
    }

    /**
     * 分页查询
     * @param pageRequest 分页查询条件
     * @return
     */
    @PostMapping("findPage")
    public Result<Page<Notice>> findPage(@RequestBody PageRequest<Notice> pageRequest)  {
        long now=System.currentTimeMillis();
        Result<Page<Notice>> result=new Result<>();
        result.setData(noticeDispatch.findPage(pageRequest.getPage(), pageRequest.getModel()));
        result.setTime(System.currentTimeMillis()-now);
        return result;
    }

    /**
     * 根据主键删除对象
     * @param ids 删除的对象主键
     * @return
     */
    @GetMapping("remove")
    public Result<Boolean> remove(@RequestParam("ids") List<Long> ids)  {
        long now=System.currentTimeMillis();
        Result<Boolean> result=new Result<>();
        result.setData(noticeDispatch.remove(ids));
        result.setTime(System.currentTimeMillis()-now);
        return result;
    }

    /**
     * 发送消息
     * @param sendDto 删除的对象主键
     * @return
     */
    @PostMapping("send")
    public Result<Boolean> send(@RequestBody SendDto sendDto)  {
        long now=System.currentTimeMillis();
        Result<Boolean> result=new Result<>();
        result.setData(noticeDispatch.send(sendDto));
        result.setTime(System.currentTimeMillis()-now);
        return result;
    }

    /**
     * 查询未读消息数量
     * @return result
     */
    @GetMapping("unread")
    public Result<Integer> unread() {
        long now=System.currentTimeMillis();
        Result<Integer> result=new Result<>();
        result.setData(noticeDispatch.unread());
        result.setTime(System.currentTimeMillis()-now);
        return result;
    }

    /**
     * 滚动分页查询消息
     * @param pageRequest
     * @return result
     */
    @PostMapping("scrollPage")
    public Result<Page<NoticeVo>> scrollPage(@RequestBody  PageRequest<UserNotice> pageRequest)  {
        long now=System.currentTimeMillis();
        Result<Page<NoticeVo>> result=new Result<>();
        result.setData(noticeDispatch.scrollPage(pageRequest.getPage(), pageRequest.getModel()));
        result.setTime(System.currentTimeMillis()-now);
        return result;
    }

    /**
     * 已读消息
     * @param
     * @return
     */
    @GetMapping("isRead")
    public Result<Boolean> isRead(@RequestParam(value = "id",required = false) Long id) {
        long now=System.currentTimeMillis();
        Result<Boolean> result=new Result<>();
        result.setData(userNoticeDispatch.isRead(id));
        result.setTime(System.currentTimeMillis()-now);
        return result;
    }

    /**
     * 当前用户消息清除所有消息
     * @param
     * @return
     */
    @GetMapping("removeMessage")
    public Result<Boolean> removeMessage()  {
        long now=System.currentTimeMillis();
        Result<Boolean> result=new Result<>();
        result.setData(userNoticeDispatch.removeByUserId(UserHolderUtils.getUserId()));
        result.setTime(System.currentTimeMillis()-now);
        return result;
    }
}